package com.cqc.gliddemo01;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.io.File;
import java.net.URI;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    public ImageView iv;

    public String url = "http://a0.att.hudong.com/10/86/19300001317260131176866103067_950.jpg";
    public String url2 = "http://img4.imgtn.bdimg.com/it/u=190405906,4010103102&fm=11&gp=0.jpg";
    public String url3 = "http://imgsrc.baidu.com/forum/w%3D580/sign=6a7704d9950a304e5222a0f2e1c9a7c3/6c0db303918fa0ec9f9214d0249759ee3f6ddbf7.jpg";
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btn7;
    private Button btn8;
    private Button btn9;
    private Button btn10;
    private Button btn11;
    private Button btn12;
    private Button btn13;
    private Button btn14;
    private Button btn15;
    private Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        initViews();
    }

    private void initViews() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btn10.setOnClickListener(this);
        btn11.setOnClickListener(this);
        btn12.setOnClickListener(this);
        btn13.setOnClickListener(this);
        btn14.setOnClickListener(this);
        btn15.setOnClickListener(this);
    }

    private void findViews() {
        iv = (ImageView) findViewById(R.id.imageView);

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);
        btn7 = (Button) findViewById(R.id.btn7);
        btn8 = (Button) findViewById(R.id.btn8);
        btn9 = (Button) findViewById(R.id.btn9);
        btn10 = (Button) findViewById(R.id.btn10);
        btn11 = (Button) findViewById(R.id.btn11);
        btn12 = (Button) findViewById(R.id.btn12);
        btn13 = (Button) findViewById(R.id.btn13);
        btn14 = (Button) findViewById(R.id.btn14);
        btn15 = (Button) findViewById(R.id.btn15);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                loadPic1();//加载资源图片
                break;
            case R.id.btn2:
                loadPic2();//加载网络图片
                break;
            case R.id.btn3:
                loadPic3();//加载文件图片
                break;
            case R.id.btn4:
                loadPic4();//占位图片
                break;
            case R.id.btn5:
                loadPic5();//动画效果
                break;
            case R.id.btn6:
                loadPic6();//裁剪
                break;
            case R.id.btn7:
                loadPic7();//加载gif
                break;
            case R.id.btn8:
                loadPic8();//加载gif作为静态图像
                break;
            case R.id.btn9:
                loadPic9();//加载视频
                break;
            case R.id.btn10:
                loadPic10();//不使用内存缓存
                break;
            case R.id.btn11:
                loadPic11();//不使用硬盘缓存
                break;
            case R.id.btn12:
                loadPic12();//优先级高
                break;
            case R.id.btn13:
                loadPic13();//优先级中
                break;
            case R.id.btn14:
                loadPic14();//优先级低
                break;
            case R.id.btn15:
                loadPic15();//优先级低
                break;
        }
    }

    private void loadPic15() {
        Glide.with(context).load(url2).priority(Priority.IMMEDIATE).into(iv);
    }

    private void loadPic14() {
        Glide.with(context).load(url2).priority(Priority.LOW).into(iv);
    }

    private void loadPic13() {
        Glide.with(context).load(url2).priority(Priority.NORMAL).into(iv);
    }

    private void loadPic12() {
        Glide.with(context).load(url2).priority(Priority.HIGH).into(iv);
    }

    private void loadPic11() {
        Glide.with(context).load(url2).diskCacheStrategy(DiskCacheStrategy.NONE).into(iv);
    }

    private void loadPic10() {
        Glide.with(context).load(url2).skipMemoryCache(true).into(iv);
    }

    private void loadPic9() {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/a.mp4";
        Uri uri = Uri.fromFile(new File(path));
        Glide.with(context).load(uri).into(iv);
    }

    private void loadPic8() {
        Glide.with(context).load(url3).asBitmap().into(iv);
    }

    private void loadPic7() {
        Glide.with(context).load(url3).asGif().into(iv);
    }

    private void loadPic6() {
        int pic_width = (int) getResources().getDimension(R.dimen.pic_width);
        int pic_height = (int) getResources().getDimension(R.dimen.pic_height);
        //直接填数字是像素，从dimens中取值是dp
        //fitCenter()  centerCrop()
        Glide.with(context).load(url2).override(pic_width, pic_height).centerCrop().into(iv);
    }

    private void loadPic5() {//Fade透明度变化
        Glide.with(context).load(url2).crossFade(3000).into(iv);
    }

    private void loadPic4() {
        Glide.with(context).load(url2).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(iv);
    }

    private void loadPic3() {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/Screenshots/a.png";
        File file = new File(path);
        Glide.with(context).load(file).into(iv);
    }

    private void loadPic2() {
        Glide.with(context).load(url).into(iv);
    }

    private void loadPic1() {
        Glide.with(context).load(R.mipmap.ic_launcher).into(iv);
    }
}
